package cn.hyatt.auth.controller;

import cn.hyatt.auth.dto.systemMenu.SystemMenuFindDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuSaveDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuUpdateDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuVo;
import cn.hyatt.auth.entity.SystemMenu;
import cn.hyatt.auth.service.SystemMenuService;
import cn.hyatt.common.exception.BusinessException;
import cn.hyatt.core.dto.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 系统菜单-控制器
 *
 * @author hyatt
 */

@ResponseBody
@RestController
@AllArgsConstructor
@RequestMapping("auth/menu")
public class SystemMenuController {

    private SystemMenuService service;

    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public R<List<SystemMenuVo>> getList(SystemMenuFindDto dto) {
        List<SystemMenu> entityList = service.findByList(dto);
        List<SystemMenuVo> voList = entityList.stream().map(service::toVo).toList();
        return R.ok(voList);
    }

    @GetMapping("/page")
    @Operation(summary = "获取分页")
    public R<IPage<SystemMenuVo>> getPage(SystemMenuFindDto dto) {
        IPage<SystemMenu> entityPage = service.findByPage(dto);
        return R.ok(entityPage.convert(service::toVo));
    }

    @GetMapping("/{id}")
    @Operation(summary = "按Id获取")
    public R<SystemMenuVo> getById(@PathVariable Long id) {
        Optional<SystemMenu> optional = service.getOptById(id);
        SystemMenu entity = optional
                .orElseThrow(() -> new BusinessException("未找到("+id+")对应的数据"));
        return R.ok(service.toVo(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public R<Void> save(@RequestBody @Validated SystemMenuSaveDto dto) {
        service.save(dto);
        return R.ok();
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新")
    public R<Void> update(@PathVariable Long id,@RequestBody SystemMenuUpdateDto dto) {
        service.update(id,dto);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public R<Void> delete(@PathVariable Long id) {
        service.deleteById(id);
        return R.ok();
    }

}
